﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.Services/ServiceProvider
* 类       名: ServiceProvider
* 功      能:  TD
* 时      间:  2023/12/26 14:58:50
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Ioc;
using System;
using System.IO;
using System.Reflection;

namespace XrayDirection.Services
{
    // 服务配置类,一些公共的设置
    public class ServiceProvider
    {
        private static IContainerProvider? _containerProvider;

        public static void SetContainerProvider(IContainerProvider? containerProvider)
        {
            _containerProvider = containerProvider;
        }

        public static string AppDir
        {
            get
            {
                // 获取当前应用程序的全路径,应用程序的根目录+应用程序.exe,类似下面这种
                // C:\Program Files\MyApp\MyApp.exe
                var location = Assembly.GetExecutingAssembly().Location;
                return location == null ? "" : Path.GetDirectoryName(location)!;
            }
        }

        /// <summary>
        /// 项目配置或者是一些设置的目录
        /// </summary>
        private static string? _settingsDir;

        public static string? SettingsDir
        {
            get
            {
                if (_settingsDir == null)
                {
                    var localSettings = Path.Combine(AppDir, "Settings");
                    _settingsDir = localSettings;

                    if (!Directory.Exists(localSettings))
                        Directory.CreateDirectory(localSettings);
                }
                return _settingsDir;
            }
            set { _settingsDir = value; }
        }

        private static string? _logDir;

        public static string? LogDir
        {
            get
            {
                if (_logDir == null)
                {
                    var localLog = Path.Combine(AppDir, "Logs");
                    _logDir = localLog;
                    if (!Directory.Exists(localLog))
                        Directory.CreateDirectory(localLog);
                }
                return _logDir;
            }
            set { _logDir = value; }
        }

        /// <summary>
        ///  获取程序集的名称和版本信息的AssemblyName对象的 Version属性,这属性是一个Version对象.
        ///  Version对象包含了四个部分的版本号信息: 主版本号,次版本号,内部版本号和修订号. 例如:
        ///  1.2.3.4 主版本是1,次版本是2,内部版本3,修订版本号4
        /// </summary>
        public static Version? AppVersion => Assembly.GetEntryAssembly()?.GetName().Version;

        public static T Get<T>()
        {
            return _containerProvider.Resolve<T>();
        }

    }
}
